Skip to content

perf(daemon-client): send opted-in standalone remote commands as one checked RPC - #5145

Merged
thymikee merged 10 commits into
perf/checked-rpc-routefrom
perf/checked-rpc-client
Nov 27, 2026
Merged

thymikee merged 10 commits into
perf/checked-rpc-routefrom
perf/checked-rpc-client

Conversation

@thymikee

@thymikee thymikee commented Nov 27, 2026 •

Copy link
Copy Markdown
Member

Summary

Stacked on #5144. Replaces closed #5134. A connection whose remote-config profile sets daemonRpc: "checked" sends a standalone command's dispatch as one POST /rpc/checked. Without it, the client path is unchanged.

  • connect probes the endpoint, requires a proxy's upstream to be the daemon itself, and records only a fingerprint of the exact checked target.
  • daemon-client sends checked only when that fingerprint names the request's own target, the request uploads nothing, it is not a lease allocate/heartbeat, and the process holds no instance-pinned /health answer. After a lease call, the command reuses that answer on fenced /rpc with no extra round trip.
  • A non-JSON-RPC 404 fails typed (remote_checked_rpc_unsupported, dispatched: unknown), never resent.

Security

  • Only resend trigger: client-internal proof the socket never connected; recovery then probes /health live.
  • No checked error or timeout diagnostic names the endpoint; unknown refusal values become unknown.
  • Pre-send refusals and recovery skew set dispatched: no.
  • retryAfterRemoteInstanceMismatch stays in the resend module: moving it back puts the transport over the 1000-line cap.

Before / after

session list after connect, n=10, http daemon behind a 50 ms each-way delay proxy:

variant                  requests/call  routes           mean   median
base 7d392e6214          2              /health + /rpc   373ms  374ms
head, no daemonRpc       2              /health + /rpc   376ms  371ms
head, daemonRpc checked  1              /rpc/checked     268ms  266ms

Validation

Head 5217b08. New tests fail without their fixes (checked by mutation). daemon-client, connection, remote, wire-compat, eager-closure, layering and fallow audit pass locally. CI pending.

🤖 Generated with Claude Code

@github-actions

github-actions Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Size Report

Metric Base Current Diff
Installed (including dependencies) 6.66 MB 6.67 MB +10.0 kB
Package (unpacked) 6.65 MB 6.66 MB +10.0 kB
Package (download) 2.03 MB 2.03 MB +3.3 kB

Startup median (7 runs, lower is better):

Scenario Base Current Diff
CLI --version 22.0 ms 20.8 ms -1.2 ms
CLI --help 46.4 ms 38.4 ms -8.0 ms

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed

Reply with feedback, questions, or to request a fix.

View guided diff | Turn on auto-fix | Re-trigger cubic

Comment thread website/docs/docs/remote-proxy.md Resolved
Comment thread website/docs/docs/remote-proxy.md Resolved
Comment thread packages/contracts/src/daemon-http.ts Resolved
@apex-sentinel-staging

This comment has been minimized.

@apex-sentinel-staging apex-sentinel-staging Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apex Sentinel: 1 new finding on 7f69b8b. The verdict and the full list are in the summary comment.

@thymikee
thymikee force-pushed the perf/checked-rpc-client branch 2 times, most recently from 142ca31 to 42e039f Compare November 27, 2026 08:11
@apex-sentinel-staging

This comment has been minimized.

@apex-sentinel-staging

This comment has been minimized.

@thymikee

Copy link
Copy Markdown
Member Author

At 8728027, nine likely problems need fixes. They affect where checked mode sends requests, which setups it accepts, what it exposes about the daemon URL, and what it records and documents. Each code fix needs a regression test that drives the described case and fails without the fix.

A second independent pass found that checked approval can likely move to a different HTTP route (remote-connection-state.ts:407). Connect approves a daemon under a non-root prefix, and a later base URL adds a query parameter named token at that prefix. The comparison strips the parameter and keeps checked mode, but the transport appends its slash after the query and resolves the route at the origin root (daemon-http.ts:83, daemon-client-transport.ts:795). The reverse happens when connect probes the query spelling and saves the sanitized form. Can approval bind to a fingerprint of the effective HTTP target, with one route resolution shared by the probe, the comparison and the send?

A second independent pass found that connect can likely approve a proxy chain without checking its last daemon (daemon-client-checked-rpc.ts:216). With an embedded proxy forwarding to another proxy, each one includes its upstream health verbatim, but the client drops the nested upstream. If the last daemon lacks checked RPC, connect succeeds and the first standalone command gets a 404. Every link through the terminal daemon must be validated, or a proxy's advertised upstream must be required to identify as a daemon.

A second independent pass found that checked failures likely attach the daemon URL to caller-visible errors (daemon-client-checked-rpc.ts:202, also lines 143, 153, 177 and 182). The new connect and recovery calls expose it too, through URL and trust validation, the health protocol comparison and remoteDaemonUnavailableError (index.ts:481, daemon-client-remote-resend.ts:34, daemon-lifecycle-errors.ts:124). SDK callers get the full value, including any embedded credential, and CLI redaction still keeps the URL and path. No error raised at the checked connect or send boundaries should carry the endpoint; the typed reason, dispatch status and validated upgrade metadata can stay.

A second independent pass found that an unknown refusal header is likely copied as-is into AppError.details.refusal (daemon-client-checked-rpc.ts:152). An intermediary can put a URL or an opaque credential there, and pattern-based redaction cannot catch every one. Only known values should reach the error; record anything else as a fixed unknown marker.

A second independent pass found that a checked request timeout likely records the endpoint in diagnostics (daemon-client-timeout.ts:215, reached from daemon-client-transport.ts:419). The host and path survive redaction into debug output and the failure log. No checked timeout diagnostic should include daemonBaseUrl; request ID, command and timeout fields can stay.

A second independent pass found that checked approval likely writes another copy of the endpoint URL into the connection record, as checkedRpcBaseUrl (connection.ts:223, remote-connection-state.ts:394). The sanitizer keeps the host, path, fragment and any query value whose key does not look like a credential. Approval should persist and compare only a fingerprint of the validated HTTP target.

A second independent pass found that direct SDK lease calls likely skip the instance fence (daemon-client-lifecycle.ts:152). With daemonRpc: checked, leases.allocate and leases.heartbeat go on the checked route without health-derived instance IDs, and the CLI override does not cover SDK clients. The transport's standalone predicate must send leaseAllocate and leaseHeartbeat health-first for every caller.

A second independent pass found that some checked opens with save-script likely keep their publication record although nothing was sent (session-script-destination.ts:57). This happens when recovery after a connect timeout finds protocol skew (daemon-client-remote-resend.ts:52, daemon-client-rpc.ts:285), and on the pre-send size refusal. Every transport branch that proves the request did not leave must set dispatched: no, keeping the typed error; ambiguous cases must keep the record.

A second independent pass found that the base compatibility guide still says remote clients always read /health before commands. After a successful checked connect, session list goes straight to POST /rpc/checked. Please describe the health probe as the default behavior and point to the checked RPC section.

Is there a simpler shape, where daemon-client owns the whole eligibility rule? It would send checked only when the connection records checked RPC for this endpoint, the request uploads nothing (one predicate exported from daemon-artifacts.ts), and the process holds no instance-pinned /health response for the endpoint. After a health-first lease call, the command would then use /rpc with fences and no extra round trip, as today. That would remove the leasePrepared plumbing, while the materialization-client override stays so lease calls learn the instance ID. Could retryAfterRemoteInstanceMismatch also stay where it was, with connect recovery in the lazy daemon-client-checked-rpc.ts, to drop RemoteResendPort and the move?

Not blocking: each of these can be taken or left: share one requestUploadsArtifacts-style predicate from daemon-artifacts.ts between the artifact preparer and isStandaloneRemoteRequest, which repeats the upload command list; make the connect docs cover an unreachable /health (remote_daemon_unavailable) and a checked profile with no resolved base URL, which connects in health mode silently (or refuse it); document that a 502, 503 or 504 on a checked POST is not waited out or resent because it may have been dispatched; type the daemonHttpEndpoint route parameter as 'health' | 'rpc' | typeof DAEMON_HTTP_CHECKED_RPC_ROUTE instead of string; and either keep daemonRpc off the public client config or tell SDK users that setting it directly leaves lease calls and commands unfenced to one daemon.

These earlier threads no longer apply and can be resolved: the stored health response is now invalidated before recovery (#5145 (comment)); the docs now separate the two dispatch requests from uploads, downloads and lease calls (#5145 (comment)); connect behavior is now covered through runCli, and the helper test is named for the capability check (#5145 (comment)); the recovery catch now honors cancellation, with a test (#5145 (comment)); the docs now separate connect from later commands for dispatch status (#5145 (comment)); the table comment no longer says pre-dispatch only (#5145 (comment)); the unreachable hint now follows its error function (#5145 (comment)); and the connect probe now applies trust validation, with a test that no request leaves on refusal (#5145 (comment)).

Not verified here: tests and mutations were not run, so the claim that the regression tests fail without their fixes was judged by reading only. The before/after bench was not re-run. It used a direct HTTP daemon, so the proxy hop has no live run in this PR and relies on unit tests here and in #5144. The server-side guarantees from #5144 were not re-checked.

The only check not passing is "Body rules", which was cancelled rather than failed. It is likely unrelated to the code, possibly replaced by a newer run, and the other 22 checks pass. It was not verified whether this check is required or why it was cancelled.

Before merge, the problems above need fixes and "Body rules" needs a re-run.

@thymikee
thymikee force-pushed the perf/checked-rpc-client branch from 8728027 to 5f95f06 Compare November 27, 2026 09:27
@thymikee

Copy link
Copy Markdown
Member Author

Thanks. Fixed in 5f95f06, rebased on #51447be2a52. Each code fix has a test that fails without it (checked by mutation).

I took the simpler shape: daemon-client owns eligibility. It sends checked only when (a) the recorded approval fingerprints this exact target, (b) requestUploadsArtifacts (exported from daemon-artifacts.ts, shared with the preparer) is false, (c) it is not lease_allocate/lease_heartbeat, for every caller including the SDK, and (d) the process holds no instance-pinned /health answer. The leasePrepared plumbing is gone, and the materialization override stays. After a lease call, the command uses fenced /rpc with no extra round trip.

  1. connect records a SHA-256 of buildDaemonHttpUrl(resolvedBase, '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/rpc/checked'), the same resolution the send uses. No URL is persisted. The query-token-under-prefix case resolves to the root, gets a different fingerprint, and goes health-first.
  2. A proxy's upstream must identify as agent-device-daemon and advertise checked-rpc. A missing upstream or a proxy behind a proxy is refused.
  3. Errors at the checked connect and send boundaries carry no endpoint: refusals, URL/trust validation, the health protocol comparison and remote_daemon_unavailable.
  4. Unknown refusal values become refusal: 'unknown'.
  5. Checked timeout diagnostics omit daemonBaseUrl.
  6. Covered by (c).
  7. The size refusal and recovery that finds skew set dispatched: no.
  8. The compatibility guide calls the probe the default and links the checked section.

Non-blocking: the route parameter is typed. The docs cover an unreachable /health and 502/503/504. A checked profile with no base URL is refused at connect. The SDK daemonRpc doc says direct use is unfenced.

Not done: retryAfterRemoteInstanceMismatch stays in the lazily loaded resend module. Moving it back puts daemon-client-transport.ts about 50 lines over the 1000-line cap, so the port module stays too.

@apex-sentinel-staging

This comment has been minimized.

@apex-sentinel-staging apex-sentinel-staging Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apex Sentinel: 1 new finding on 5f95f06. The verdict and the full list are in the summary comment.

@apex-sentinel-staging

This comment has been minimized.

@thymikee

Copy link
Copy Markdown
Member Author

At 976d47f, all nine blocking findings from the earlier review of 8728027 are closed, but one problem remains in lease materialization.

A second independent pass found a likely failure when a leased session is connected while the proxy and daemon support checked RPC, and the daemon is then rolled back to a protocol-compatible version that serves lease RPCs without an instance ID, with the same endpoint and profile. In connection-runtime.ts, a leased open can allocate or renew through /rpc, but that health answer cannot be pinned. Checked mode stays in the materialized flags, so the open itself goes to /rpc/checked and fails at the older daemon, after the lease already succeeded. The base and the 8728027 lease path both send this open through /rpc. After CLI lease materialization, the command must stay on /rpc whenever the lease's health answer cannot be pinned, and a complete answer should still be reused. To validate, run this state with a health answer that has no instance ID, and show that both the lease call and the following open succeed through /rpc.

Not blocking: these can be taken or left: remote-proxy.md could say "Only a connection attempt that timed out before it opened is retried, after /health answers again; a refused connection fails at once", and the new daemonRpcTarget JSDoc in client-connection.ts could move above /** See DaemonLifecycle. */ so that daemonLifecycle keeps its doc.

This earlier thread no longer applies and can be resolved: a refused connection now skips the health retry loop, and the test "a refused checked POST fails at once even while /health answers 503" covers it.

Not verified here: tests and mutations were not run, and whether each new test fails without its fix was judged by reading the code at 987dc5a (the rebased 8728027). Not verified here: the before/after bench was not re-run, and this PR has no live run through a proxy hop. Not verified here: the server-side behavior from the base PR #5144 was not re-reviewed, and the base movement from 8efd3fb to 7be2a52 was not reviewed as part of this PR.

The 'Body rules' check was cancelled, not failed, and this is likely unrelated to the code: it validates the PR body and template, and no file in the diff is on its route. The other 22 checks pass at 976d47f. Not verified here: whether 'Body rules' is a required check.

Before merge, the lease problem needs a fix and 'Body rules' needs a re-run.

thymikee and others added 9 commits November 27, 2026 11:07
…checked RPC

A connection with daemonRpc: checked (profile field, --daemon-rpc, or
AGENT_DEVICE_DAEMON_RPC) sends standalone commands on POST /rpc/checked
with no /health probe first. connect accepts the field only when every
link advertises checked-rpc. Installs keep health-first and the
instance fences. A route miss fails typed with dispatched unknown and is
never resent; refusals keep today's upgrade message. Only a connection
that never opened is acted on: refused fails at once, a connect timeout
runs the existing /health recovery loop and sends once. Without the
field the client path is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…se flows fenced

- Only connect enables checked RPC: daemonRpc is a remote-config profile
  field with no flag or environment variable, recorded in the connection
  after every link proves checked-rpc; a proxy must prove its upstream.
  The connect probe goes through the client's URL and trust validation.
- A command that allocates or renews its lease first, and the lease
  calls themselves, go health-first with the instance fences.
- Checked connect recovery decides on fresh /health probes, and an abort
  during it rejects with the typed cancellation.
- A protocol refusal keeps its typed reason and the refusing side; an
  unknown refusal value is an unreadable reply, never resent.
- Script-record cleanup on proven non-dispatch applies to checked opens
  only. The resend rules take transport powers as a port, so the lazily
  loaded modules import nothing back.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…he resend rules

The transport type-imported RemoteResendPort from the resend module,
which closed a 9-file type cycle through the checked-RPC, recovery and
lifecycle modules (R9/R10). The port and the health facts it reads now
live in daemon-client-remote-resend-port.ts, which imports neither side.
The SDK daemonRpc field documents that setting it is an explicit opt-in
the server enforces.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…obe live in recovery

- The recorded checked mode holds only for the base URL connect
  validated; a command that names another endpoint goes health-first.
- Checked connect recovery probes /health through the live reader, so a
  concurrent call that refills the cache never stands in for its probe.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… probed

connect now records the sanitized URL it probed (checkedRpcBaseUrl) in
place of a bare mode, and checked RPC applies only when a command's
effective endpoint, from its flags or the environment, sanitizes to
that URL. A connection with no probed URL, such as a direct-provider
one, never applies checked mode to a URL that arrives later. Equivalent
spellings (host case, default port, trailing slash) keep checked mode
through the existing URL sanitizer.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… the endpoint out of errors

- daemon-client decides alone: checked only when the connection's
  recorded approval fingerprints this exact target (one route
  resolution, shared with the send), the request uploads nothing
  (requestUploadsArtifacts, shared with the artifact preparer), it is
  not a lease allocate or heartbeat, and the process holds no
  instance-pinned /health answer. The leasePrepared plumbing is gone.
- connect records only the target fingerprint, refuses a checked
  profile with no base URL, and requires a proxy's upstream to be the
  daemon itself.
- No error at the checked connect or send boundary, and no checked
  timeout diagnostic, names the endpoint. An unknown refusal value is
  recorded as 'unknown'.
- The pre-send size refusal and recovery that finds protocol skew set
  dispatched: no.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…connect verdict

The recovery loop now takes the observed verdict as its first failed
probe: a refused connection is not transient and fails at once with
remote_daemon_unavailable and dispatched: no, and only a connect that
timed out enters the probe loop. Adds a regression test for a refused
checked POST while /health answers 503.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…not be pinned

A process that read /health for an endpoint and got an answer with no
complete instance ID, such as a lease call's against a rolled-back
daemon, now sends later commands health-first instead of on the checked
route; a pinned answer is still reused, fenced and free. Docs say a
refused connection fails at once, and daemonLifecycle keeps its own doc.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@thymikee
thymikee force-pushed the perf/checked-rpc-client branch from 976d47f to 18e86fc Compare November 27, 2026 10:10
@thymikee

Copy link
Copy Markdown
Member Author

Fixed in 18e86fc, rebased on #51441633e10. The process health cache now also remembers an endpoint whose last reachable /health answer could not be pinned (no complete instance ID). daemon-client sends checked only when the process has read no /health answer for that endpoint. A pinned answer is still reused, fenced and with no extra round trip. An unpinnable answer means the command goes health-first again, as before. This covers the leased open against a daemon rolled back to a version with no instance ID. New test: a lease heartbeat against a daemon that reports no instance ID, then a checked-eligible command. It sends GET /health, POST /rpc twice and never /rpc/checked, and fails without the fix (checked by mutation).

Non-blocking items applied: the remote-proxy.md wording ("Only a connection attempt that timed out before it opened is retried, after /health answers again; a refused connection fails at once"), and the daemonRpcTarget JSDoc now sits after daemonBinding, so daemonLifecycle keeps its own doc.

@apex-sentinel-staging

This comment has been minimized.

@thymikee

Copy link
Copy Markdown
Member Author

At 18e86fc, the lease case from the earlier review is fixed for the ordinary health probe. The stale-instance retry can still send the following open to /rpc/checked.

A second independent pass found this likely defect. A leased proxy open has a saved checked approval and an exact device ID, and device lookup can pin health for the old proxy and daemon. The proxy then restarts at the same URL before lease allocation, in front of an older, protocol-compatible daemon, and rejects the old instance ID. The retry clears the old pin, reads the new health answer without an upstream instance ID, and allocates through /rpc. It writes that answer through cacheRemoteDaemonHealth, which discards an unpinnable answer and bypasses the new marker. So the following open selects /rpc/checked and fails if the older daemon lacks that route. At base 1633e10, this open uses /rpc.

Every reachable unpinnable health answer must be recorded, whichever caller of cacheRemoteDaemonHealth supplies it. Recording it inside that function covers both the ordinary probe and the retry. Please keep the probe generation guard in cachedRemoteHealth and keep reusing complete pins. To validate, run a leased open through the old-instance rejection to a replacement daemon that reports no instance ID. The output should show the following open sent to /rpc and succeeding.

CI is green at 18e86fc, including the re-run of Body rules.

Not verified here: tests and mutations were not run, and there was no live run against a daemon that reports no instance ID. Whether the new test fails without the fix was judged only by reading the code at bc7c806. The CLI route from materializeRemoteConnectionForCommand to the leased open was traced only by reading, and the new test drives lease_heartbeat and session list through sendToDaemon instead. It was not checked whether the built bundle keeps one instance of daemon-client-remote-health-cache.ts across static and dynamic imports. The base changes from 7be2a52 to 1633e10 and the server side of #5144 were not reviewed.

Before merge, the retry case needs a fix, and the base PR #5144 has to merge.

… is cached

cacheRemoteDaemonHealth now remembers a reachable answer with no
complete instance ID itself, so the stale-instance retry registers it
as the ordinary probe does: after a retry onto a daemon that reports no
instance ID, the next checked-eligible command stays health-first. The
probe generation guard and complete-pin reuse are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@thymikee

Copy link
Copy Markdown
Member Author

Fixed in 5217b08. cacheRemoteDaemonHealth now records a reachable answer that names no complete instance ID itself, so the stale-instance retry registers it the same way the ordinary probe does. The probe generation guard in cachedRemoteHealth is unchanged, and complete pins are still reused.

New test: a leased flow first pins instance old. The daemon is then replaced by one that reports no instance ID, rejects old on /rpc with 409, and answers /rpc/checked with 404. The lease heartbeat retries through the rejection onto the replacement. The following open sends GET /health, POST /rpc and succeeds. On 18e86fc the same test sends /rpc/checked and fails.

@apex-sentinel-staging

Copy link
Copy Markdown

Apex Sentinel · ✅ Approve · review of 5217b08

Since the review of 18e86fc: 0 new · 0 still open · 0 no longer raised

No open findings.

1 finding below minor hidden (user_settings.min_severity).

1 new finding away from what changed since 18e86fc not shown. Comment @apex-sentinel-staging review for a full review.

Reviewed with this repository's own Sentinel configuration (7d392e62143f).

💬 Talking to Sentinel

In a finding's thread, reply with:

Command What it does
@apex-sentinel-staging note <context> Adds context; Sentinel re-checks the cited code
@apex-sentinel-staging pivot <why> Says the finding is wrong; Sentinel re-checks the cited code
@apex-sentinel-staging like Marks the finding as useful
@apex-sentinel-staging dislike <why> Marks it as not useful, so it is not raised again

On this conversation, comment @apex-sentinel-staging review to run a full review once again.

@thymikee

Copy link
Copy Markdown
Member Author

No blocking problems were found at 5217b08. The stale-instance retry case from the earlier review is fixed: a reachable health answer without complete instance IDs is now recorded for both the ordinary probe and the retry, so the following open reads health again and uses /rpc. The probe generation guard and the reuse of complete pins are unchanged.

Not verified here: tests and mutations were not run, and there was no live run against a real proxy or daemon that reports no instance ID. That the new test fails at 18e86fc was judged only by reading the code; it uses a local HTTP stand-in through the real sendToDaemon transport. It was not checked whether the built bundle keeps a single instance of the remote health module across static and dynamic imports. The server side of #5144 was not reviewed.

The only check not passing is Body rules, which was cancelled. It validates the PR description, which did not change since 18e86fc, and it does not exercise the changed code, so the cancellation looks unrelated.

Before merge, Body rules needs a re-run, and the base PR #5144 has to merge.

@thymikee thymikee added the ready-for-human Valid work that needs human implementation, judgment, or maintainer merge label Nov 27, 2026
@thymikee
thymikee merged commit 9076150 into main Nov 27, 2026
22 of 23 checks passed
@thymikee
thymikee deleted the perf/checked-rpc-client branch November 27, 2026 13:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready-for-human Valid work that needs human implementation, judgment, or maintainer merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant